package main

import (
	"encoding/xml"
	"fmt"
)

func main() {
	fmt.Println("examp26")
	var result = Result{
		Person: []Person{
			Person{"张三", 22, "程序员"},
			Person{"李四", 25, "DBA"},
			Person{"王五", 21, "农民"},
			Person{"赵六", 26, "工人"},
		},
	}
	xmlStr, err := xml.MarshalIndent(result, " ", " ")
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("XML数据：\n", string(xmlStr))
	//将xml转换结构
	result = Result{}
	fmt.Println("重新给result一个空结构：", result)
	xml.Unmarshal(xmlStr, &result)
	fmt.Printf("转换结果：%+v", result)
}

//结构里的字段首字母需要大写
type Result struct {
	Person []Person `xml:"person"`
}
type Person struct {
	Name   string `xml:"name,attr"` //标签名称,attr写在属性里
	Age    int    `xml:"age,attr"`
	Career string `xml:"career"` //标签名写在标签里
}
